package com.medicine.hbp.ui;

import org.kymjs.kjframe.http.HttpParams;
import org.kymjs.kjframe.ui.BindView;

import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.medicine.hbp.adapter.ArtMeAdapter;
import com.medicine.hbp.base.BaseActivity;
import com.medicine.hbp.http.AppHttpCallBack;
import com.medicine.hbp.http.AppHttps;
import com.medicine.hbp.http.HttpInterface;
import com.medicine.hbp.mob.ArtMeInfoVo;
import com.medicine.hbp.mob.FeedbackVo;
import com.medicine.hbp.sp.LoginSp;
import com.medicine.hbp.uitl.CommUtil;
import com.medicine.hbp.uitl.ToastUtil;
import com.medicine.hbp.xListView.XListView;
import com.medicine.hbp.xListView.XListView.IXListViewListener;
import com.medicine.hbp.R;

public class ArtMeActivity extends BaseActivity implements IXListViewListener {
	@BindView(id = R.id.xListView_feedback, click = false)
	public XListView xListView;
	@BindView(id = R.id.btn_feedback, click = false)
	public Button mBtnArtMe;
	@BindView(id = R.id.edit_feedback, click = false)
	public EditText mEditArtMe;

	private volatile int page = 1;// 当前页（从0开始）
	private int pageSize = 10;
	private ArtMeAdapter adapter;
	private int userid;
	@Override
	public void setRootView() {
		super.setRootView();
		setContentView(R.layout.activity_artme);
	}
	@Override
	public void initWidget() {
		super.initWidget();
		initTop();
		userid=getIntent().getIntExtra("UserId", -1);
		adapter = new ArtMeAdapter(aty);
		xListView.setAdapter(adapter);
		xListView.setXListViewListener(this);
		xListView.setPullLoadEnable(false);
		mBtnArtMe.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				httpAddOpinion();
			}
		});
		httpGetOpinion();
	}
	private void initTop() {
		TopView topview = (TopView) findViewById(R.id.topview);
		topview.setTitle("@我");
		topview.setLeftVisibility(View.GONE);
		topview.setLeftVisibilityExit(View.VISIBLE);
		topview.setRightVisibility(View.INVISIBLE);
		topview.setLeftOnClickExit(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
	}
	/**
	 * @我发送信息接口
	 */
	private void httpAddOpinion() {
		final String edit=mEditArtMe.getText().toString();
		if(TextUtils.isEmpty(edit)){
			ToastUtil.showShort(aty, "您还没有填写意见哦！");
			return;
		}
		HttpParams params = new HttpParams();
		params.put("msg", edit);
		params.put("userId", userid);
		AppHttps.HttpPost(this,HttpInterface.ADD_MESSAGE, params,
				new AppHttpCallBack<Object>(Object.class) {
					@Override
					public void onSuccess(Object result) {
						ArtMeInfoVo items=new ArtMeInfoVo();
//						items.msgDate=Integer.valueOf(CommUtil.formatDate(System.currentTimeMillis()));
						items.msgInformation=edit;
						items.userId=LoginSp.getUserId(ArtMeActivity.this);
						adapter.addItem(items);
						mEditArtMe.setText("");
						xListView.setSelection(xListView.getBottom());
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(aty, errorMsg);
					}
				});

	}

	/**
	 * @我详细信息接口
	 */
	private void httpGetOpinion() {
		HttpParams params = new HttpParams();
//		params.put("current", page);
//		params.put("rowCount", pageSize);
		params.put("userId", userid);
		AppHttps.HttpPost(this,HttpInterface.FIND_MESSAGE_INFO, params,
				new AppHttpCallBack<FeedbackVo>(FeedbackVo.class, aty) {
					@Override
					public void onSuccess(FeedbackVo result) {
						boolean isbo=true;
						if(page==1){
							adapter.setItems(result.messageInfo);
							xListView.setSelection(xListView.getCount()-1);
						}else{
							adapter.addItems(result.messageInfo);
						}
						adapter.notifyDataSetChanged();
						if(result.messageInfo.size()<10){
							isbo=false;
						}
						xListView.setPullRefreshEnable(isbo);
						if(page==1){
							xListView.setSelection(xListView.getBottom());
						}
					}
					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(aty, errorMsg);
					}
					@Override
					public void onFinish() {
						super.onFinish();
						xListView.setRefreshTime(CommUtil.formatDate(System.currentTimeMillis()));
					}
				});
	}
	@Override
	public void onRefresh() {
		page++;
		httpGetOpinion();
	}
	@Override
	public void onLoadMore() {

	}
}
